<?php
/*
  $Program: ECJC Online Shop
  $Id: checkout_confirmation.php
  $Based on: osCommerce, Open Source E-Commerce Solutions
  Released under the GNU General Public License
*/
  defined( 'joscom' ) or die( 'Direct Access to this location is not allowed.' );
  if (defined( '_OSCOMMERCE_LOADED' )) {
    if (!DB_SHARED) {
      tep_db_connect() or die('Unable to connect to database server!');
    }
    global $category_links, $languages_id, $catID, $catID_array, $current_category_id;
    global $request_type, $lng, $cart, $currencies, $language, $navigation, $breadcrumb, $order_total;
  } else {
    require('components/'.$option.'/includes/application_top.php');
  }


  require(DIR_WS_LANGUAGES . $language . '/' . $osMod . '.php');
global $cc_id;
// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id') && $ecjc_pwa != 1) {
    $navigation->set_snapshot(array('mode' => 'SSL', 'page' => FILENAME_CHECKOUT_PAYMENT));
    tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }
 if (!tep_session_is_registered('billto')) {
    tep_session_register('billto');
    $billto = $customer_default_address_id;
  } else {

    
    if ( (is_array($billto) && empty($billto)) || is_numeric($billto) ) { 
        $check_address_query = tep_db_query("select count(*) as total from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . (int)$customer_id . "' and address_book_id = '" . (int)$billto . "'"); 
        $check_address = tep_db_fetch_array($check_address_query); 


       if ($check_address['total'] != '1') { 
            $billto = $customer_default_address_id; 
           if (tep_session_is_registered('payment')) tep_session_unregister('payment'); 
         } 

    }
  }
// if there is nothing in the customers cart, redirect them to the shopping cart page
  if ($cart->count_contents() < 1) {
    tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
  }

// avoid hack attempts during the checkout procedure by checking the internal cartID
  if (isset($cart->cartID) && tep_session_is_registered('cartID')) {
    if ($cart->cartID != $cartID) {
      tep_redirect(tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
    }
  }


  if (!tep_session_is_registered('payment')) tep_session_register('payment');
  if (isset($_POST['payment'])) $payment = $_POST['payment'];

  if (!tep_session_is_registered('comments')) tep_session_register('comments');
  if (tep_not_null($_POST['comments'])) {
    $comments = tep_db_prepare_input($_POST['comments']);
  }

// load the selected payment module
  require(DIR_WS_CLASSES . 'payment.php');
  
  
  if ($credit_covers) $payment=''; // CCGV
  $payment_modules = new payment($payment);
  require(DIR_WS_CLASSES . 'order_total.php');// CCGV


  require(DIR_WS_CLASSES . 'order.php');
  $order = new order;

  $payment_modules->update_status();
  $order_total_modules = new order_total;// CCGV
  //$order_total_modules->process(); //Moved for osC2.2 RC1 update
  $order_total_modules->collect_posts();// CCGV
  $order_total_modules->pre_confirmation_check();//  CCGV
  


//$order_total_modules->process();

 $customer_shopping_points_spending = $_POST['customer_shopping_points_spending'];
 $point_value = tep_calc_shopping_pvalue($customer_shopping_points_spending);
//die($customer_shopping_points_spending);

  if (isset($_POST['customer_shopping_points_spending']) && USE_REDEEM_SYSTEM == 'true') {
  if (tep_session_is_registered('cot_gv') && ($cot_gv=='on')){
(tep_calc_shopping_pvalue($customer_shopping_points_spending)<=($order->info['total']-$gv_payment_amount)) ? $ccgvpoints_covers = false : $ccgvpoints_covers =true;

}



    if (isset($_POST['customer_shopping_points_spending'])&&tep_calc_shopping_pvalue($customer_shopping_points_spending) < $order->info['total'] && !is_object($$payment) && !$ccgvpoints_covers) {
    
     if (isset($_POST['payment'])) {
    $payment = $_POST['payment'];
    
    
    }else{
   //   tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(REDEEM_SYSTEM_ERROR_POINTS_NOT), 'SSL'));
      }
      
    } else {
    if (!tep_session_is_registered('customer_shopping_points_spending'))
    
      tep_session_register('customer_shopping_points_spending');
      if(tep_calc_shopping_pvalue($customer_shopping_points_spending) >= $order->info['total']){
      $payment = '';
      //$dblcheckpoints = true;
    
      }
    }
    
  }
  //$payment_modules->update_status();
  //$order_total_modules = new order_total;// CCGV
  //$order_total_modules->process(); //Moved for osC2.2 RC1 update
 // $order_total_modules->collect_posts();// CCGV
 // $order_total_modules->pre_confirmation_check();
  $order_total_modules->process();

  if ($order->info['total'] == 0){
  $dblcheckpoints = true;
}

  if (isset($_POST['customer_referred']) && tep_not_null($_POST['customer_referred'])) {
    $valid_referral_query = tep_db_query("SELECT customers_id FROM " . TABLE_CUSTOMERS . " WHERE customers_email_address = '" . $_POST['customer_referred'] . "'");
	$valid_referral = tep_db_fetch_array($valid_referral_query);
    if (!tep_db_num_rows($valid_referral_query)) {
      tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(REFERRAL_ERROR_NOT_FOUND), 'SSL'));
    }
    if ($_POST['customer_referred'] == $order->customer['email_address']) {
      tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(REFERRAL_ERROR_SELF), 'SSL'));
    } else {
      $customer_referral = $valid_referral['customers_id'];
      if (!tep_session_is_registered('customer_referral')) tep_session_register('customer_referral');
    }
  }
  global $$payment;
  
// if ( (is_array($payment_modules->modules)) && (sizeof($payment_modules->modules) > 1) && (!is_object($$payment)) && (!$credit_covers) ) {
  // if ( (is_array($payment_modules->modules)) && (sizeof($payment_modules->modules) > 1) && (!is_object($$payment)) && (!$credit_covers) && (!$customer_shopping_points_spending) ) {
  if ( is_array($payment_modules->modules) && (sizeof($payment_modules->modules) > 1) && !is_object($$payment) && (!$credit_covers) && (!$point_covers)  && (!$dblcheckpoints)) {
     tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_NO_PAYMENT_MODULE_SELECTED), 'SSL'));
   }
   
   //redone:::
if(!$payment){
if($order->info['total'] > 0){
  //oh shit we should not be here
    tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_NO_PAYMENT_MODULE_SELECTED), 'SSL'));
    }else{
  $payment = 'freeofcharge';
  global $$payment;
  }  
    
}
########  Points/Rewards Module V2.00 EOF #################*

  if (is_array($payment_modules->modules)) {
    $payment_modules->pre_confirmation_check();
  }

   if (($total_weight > 0 ) || (SELECT_VENDOR_SHIPPING == 'true')){
        require(DIR_WS_CLASSES . 'vendor_shipping.php');
      } elseif (($total_weight > 0 ) || (SELECT_VENDOR_SHIPPING == 'false')){
        require(DIR_WS_CLASSES . 'shipping.php');
      } else {
        require(DIR_WS_CLASSES . 'shipping.php');
        }
      $shipping_modules = new shipping($shipping);	

  $any_out_of_stock = false;
  if (STOCK_CHECK == 'true') {
    for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
      if (tep_check_stock($order->products[$i]['id'], $order->products[$i]['qty'])) {
        $any_out_of_stock = true;
      }
    }
    // Out of Stock
    if ( (STOCK_ALLOW_CHECKOUT != 'true') && ($any_out_of_stock == true) ) {
      tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
    }
  }

  $breadcrumb->add(NAVBAR_TITLE_1, tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
  $breadcrumb->add(NAVBAR_TITLE_2);
?>
<?php
IF (USE_TERMS_CONFIRM == 'true') {
?>
<script language="javascript"><!--
function rowOverEffect(object) {
  if (document.checkout_confirmation.elements[object].parentNode.parentNode.className != 'moduleRowSelected') {
    document.checkout_confirmation.elements[object].parentNode.parentNode.className = 'moduleRowOver';
  }
}

function rowOutEffect(object) {
  if (document.checkout_confirmation.elements[object].checked) {
    document.checkout_confirmation.elements[object].parentNode.parentNode.className = 'moduleRowSelected';
  } else {
    document.checkout_confirmation.elements[object].parentNode.parentNode.className = 'infoBoxContents';
  }
}

function checkboxRowEffect(object) {
  document.checkout_confirmation.elements[object].checked = !document.checkout_confirmation.elements[object].checked;
  if(document.checkout_confirmation.elements[object].checked) {
    document.checkout_confirmation.elements[object].parentNode.parentNode.className = 'moduleRowSelected';
  } else {
    document.checkout_confirmation.elements[object].parentNode.parentNode.className = 'moduleRowOver';
  }
}

function check_agree(TheForm) {
  if (TheForm.agree.checked) {
    return true;
  } else {
    alert(unescape('<?php echo CONDITION_AGREEMENT_ERROR; ?>'));
    return false;
  }
}
var win = null;
function NewWindow(mypage,myname,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
win = window.open(mypage,myname,settings)
}
//--></script>
<?php
}
require(WS_TEMPLATES . E_TEMPLATE_FOLDER . '/'.$osMod.'.html');
require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->

<?php
  require(DIR_WS_INCLUDES . 'application_bottom.php');

  // -- webMS Modification --
  // reset the Mambo Database connection (stomped by osCommerce Connection)
  if (!DB_SHARED) {
    global $database, $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix;
    $database = new database( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
  }

 ?>
